using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Map2.MapFolder
{
    class MouseEventMapPanel
    {
        private List<Building> buildings;
        private Point pointMove=Point.Empty;
        private Point pointClick = Point.Empty;
        private bool btMouseDown = false;
        private int noBuilding = -1, oldX, oldY;
        private MapPanel mapPanel;


        public MouseEventMapPanel(List<Building> buildings)
        {
            this.buildings = buildings;
        }

        public void mouseDown(int x, int y,MouseEventArgs e,MapPanel mapPanel)
        {
            if (e.Button == MouseButtons.Left)
            {
                foreach (Building b in buildings)
                {
                    pointMove = b.pointDowntheMouse(x,y);

                    if (pointMove == Point.Empty)
                    {
                        if (b.isInPosition(x,y))
                        {
                            btMouseDown = true;
                            noBuilding = buildings.IndexOf(b);
                            oldX = x;
                            oldY = y;
                            mapPanel.Cursor = Cursors.SizeAll;
                            break;
                        }
                    }
                    else
                    {
                        noBuilding = buildings.IndexOf(b);
                        btMouseDown = true;
                        oldX = x;
                        oldY = y;
                        mapPanel.Cursor = Cursors.SizeNESW;
                        break;
                    }
                }
            }
        }

        public void mouseMove(int x, int y,MapPanel mapPanel)
        {
            if (btMouseDown)
            {
                if (pointMove != Point.Empty)
                {
                    buildings[noBuilding].movePoint(pointMove, x - oldX, y - oldY);
                    oldX = x;
                    oldY = y;
                    pointMove = buildings[noBuilding].pointDowntheMouse(oldX, oldY);
                }
                else if (noBuilding != -1)
                {
                    buildings[noBuilding].translate(x - oldX, y - oldY);
                    oldX = x;
                    oldY = y;
                }
                mapPanel.drawBuildings();
                mapPanel.drawTextPosition(x,y);
            }
        }

        public void mouseUp(MapPanel mapPanel)
        {
            pointMove = Point.Empty;
            btMouseDown = false;
            mapPanel.Cursor = Cursors.Default;
            mapPanel.drawBuildings();
        }

        public void mouseClick(int x,int y,Point mousePosition,MouseEventArgs e,MapPanel panel)
        {
            pointClick = new Point(x, y);
            mapPanel = panel;
            if (e.Button == MouseButtons.Right)
            {
                ContextMenuStrip menuStrip = new ContextMenuStrip();

                foreach (Building b in buildings)
                {
                    if (b.isInPosition(x,y))
                    {
                        menuStrip.Items.Add("Delete");
                        noBuilding = buildings.IndexOf(b);
                        break;
                    }
                    noBuilding = -1;
                }
                if (buildings.Count == 0)
                    noBuilding = -1;
                if (noBuilding == -1)
                    menuStrip.Items.Add("New Building");
                menuStrip.Items.Add("Exit");
                menuStrip.Show(mousePosition);
                menuStrip.ItemClicked += new ToolStripItemClickedEventHandler(menuStrip_ItemClicked);
            }
        }

        private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.ToString().Equals("Exit"))
                MessageBox.Show("Exit");
            if (e.ClickedItem.ToString().Equals("Delete"))
            {
                buildings.RemoveAt(noBuilding);
                mapPanel.drawBuildings();                
            }
            if (e.ClickedItem.ToString().Equals("New Building"))
            {
                List<Point> liste = new List<Point>();
                liste.Add(new Point(pointClick.X - 15, pointClick.Y - 15));
                liste.Add(new Point(pointClick.X + 15, pointClick.Y - 15));
                liste.Add(new Point(pointClick.X + 15, pointClick.Y + 15));
                liste.Add(new Point(pointClick.X - 15, pointClick.Y + 15));
                buildings.Add(new Building(liste.ToArray()));
                mapPanel.drawBuildings();
            }

        }

        public void setBuildings(List<Building> b)
        {
            buildings = b;
        }
    }
}
